from django.core.paginator import Paginator
from django.db.models import Q
from rest_framework.response import Response
from rest_framework.views import APIView

from jiuyun.serializer import InformSerializer
from user.models import Staff, Inform


class InformView(APIView):
    def get(self,request,format=None):
        # 查看未读信息
        page = int(request.GET.get('page', '1'))
        size = int(request.GET.get('size', '10'))
        staff = Staff.objects.get(pk=request.user.pk)
        data = Inform.objects.filter(Q(staff_num=staff.num) & Q(status='未读'))
        paginator = Paginator(data, size)
        pager = paginator.page(page)
        # 模型实例化
        approve = InformSerializer(pager.object_list, many=True)
        return Response({
            'data': approve.data,
            'total': paginator.count,
            'page': pager.number,  # page当前页号
            'pageSize': size,
            'pages': paginator.num_pages  # 总页数
        })


class InformDetailView(APIView):
    def get(self,request,pk,format=None):
        # 查看未读详情并修改状态
        inform = Inform.objects.get(pk=pk)
        inform.status = '已读'
        inform.save()
        inf_detail = InformSerializer(inform,many=False)
        return Response({'data':inf_detail.data})